// miniprogram/pages/newProject/newProject.js
var app = getApp();
const host = app.globalData.host;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    projectName:"",
    projectCity:"",
    projectBlock:"",
    reCreate: false,
    projectId: null,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.projectId){
      //如果传来了projectId 则为重新创建
      this.setData({
        reCreate: true,
        projectId: options.projectId,
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  createButton: function(e){
    console.log("创建项目")
    if(this.data.projectName === ""
    || this.data.projectCity === ""
    || this.data.projectBlock === ""){
      //不可点击
      return;
    }
    console.log("可以创建")
    wx.showLoading({
      title: '加载中',
    })
    var app = getApp()
    app.newProjectDict = {
      reCreate: this.data.reCreate //如果是重新创建项目 这里要改成true
    }
    //发送请求 完成后
    //wx.redirectTo({
    //  url: 'size/size',
    //});
    var that = this;
    wx.request({
      url: host + 'city360/newProject',
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      data:{
        projectName: that.data.projectName,
        creatorOpenid: app.globalData.openid,
        projectStatus: "方案设计阶段",
        projectCity: that.data.projectCity,
        projectStreetBlock: that.data.projectBlock,
        mainProject: 'true',
        createTimeStamp: Date.now().toString()
      },
      complete: res=>{
        console.log("生成项目", res);
        wx.hideLoading();
        wx.redirectTo({
          url: '../index/index',
        });
      }
    });
  },

  nameInput:function(e){
    this.setData({
      projectName: e.detail.value,
    })
  },
  cityInput: function(e){
    this.setData({
      projectCity: e.detail.value,
    })
  },
  blockInput: function(e){
    this.setData({
      projectBlock: e.detail.value,
    })
  },
  

  back: function(e){
    wx.redirectTo({
      url:'../index/index',
    });
  }
})